package reesoft.certificate;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import reesoft.common.config.BaseConfig;
import reesoft.common.util.ConvertUtil;

/**
 * 数字证书配置参数
 *
 * Created by reesoft on 2017-08-11
 */
@Configuration
@ConfigurationProperties(prefix = "certificate")
public class CertificateConfig extends BaseConfig
{
    // 包含要加载证书信息的配置文件路径
    private String configFilePath;

    // 包含密钥信息的配置文件路径
    private String secretKeyFilePath;

    // 用 RSA 算法签名或验证签名时，是否先计算数据的摘要码再用签名（可减少签名时间）
    private boolean hashBeforeSign = true;

    // 签名时使用的证书标识
    private String signAppId;

    // 默认的签名算法
    private String signType = "MD5";

    public String getConfigFilePath()
    {
        return configFilePath;
    }

    public void setConfigFilePath(String configFilePath)
    {
        this.configFilePath = configFilePath;

        publishConfigUpdate("certificate.configFilePath", configFilePath);
    }

    public String getSecretKeyFilePath()
    {
        return secretKeyFilePath;
    }

    public void setSecretKeyFilePath(String secretKeyFilePath)
    {
        this.secretKeyFilePath = secretKeyFilePath;

        publishConfigUpdate("certificate.secretKeyFilePath", secretKeyFilePath);
    }

    public boolean isHashBeforeSign()
    {
        return hashBeforeSign;
    }

    public void setHashBeforeSign(boolean hashBeforeSign)
    {
        this.hashBeforeSign = hashBeforeSign;

        publishConfigUpdate("certificate.hashBeforeSign", String.valueOf(hashBeforeSign));
    }

    public String getSignAppId()
    {
        return signAppId;
    }

    public void setSignAppId(String signAppId)
    {
        this.signAppId = signAppId;

        publishConfigUpdate("certificate.signAppId", signAppId);
    }

    public String getSignType()
    {
        return signType;
    }

    public void setSignType(String signType)
    {
        this.signType = signType;

        publishConfigUpdate("certificate.signType", signType);
    }

    @Override
    public void onConfigUpdate(String configItemName, String value)
    {
        if ("certificate.configFilePath".equalsIgnoreCase(configItemName))
        {
            configFilePath = value;
        }
        else if ("certificate.secretKeyFilePath".equalsIgnoreCase(configItemName))
        {
            secretKeyFilePath = value;
        }
        else if ("certificate.hashBeforeSign".equalsIgnoreCase(configItemName))
        {
            hashBeforeSign = ConvertUtil.stringToBoolean(value, hashBeforeSign);
        }
        else if ("certificate.signAppId".equalsIgnoreCase(configItemName))
        {
            signAppId = value;
        }
        else if ("certificate.signType".equalsIgnoreCase(configItemName))
        {
            signType = value;
        }
    }
}
